
// A1a_summarize_m1
//==============================================================================

// Description: This file formats and summarizes the estimates for parameter m1

clear
eststo clear
set more off

cd "D:\data_replication"


// Import data and obtain country averages
//------------------------------------------------------------------------------

eststo clear
use statistics_and_graphs\costs\costs_summary.dta, clear
keep if mc_flag == 1

keep if partner == 1 | partner == 3 | partner == 4 | partner == 5 | partner == 6 | ///
partner == 7 | partner == 8 | partner == 9 | partner == 10 | partner == 11 | ///
partner == 17 | partner == 18 | partner == 30 | partner == 32 | partner == 38 | /// 
partner == 46 | partner == 53 | partner == 54 | partner == 55 | partner == 60 | /// 
partner == 61 | partner == 66 | partner == 68 | partner == 91 | partner == 600  

// Remove outliers
sum m1, detail
drop if m1 < -23.43539
drop if m1 > 25.4026

// Create Fixed Effects
gen ptq = product_id * 100000 + year * 10  + quarter + declarant * 100

xtset ptq
eststo: xtreg m1 i.partner, fe

esttab using statistics_and_graphs\costs\output_files\m1_partner_EU.csv, replace wide label nostar ///
title(Regression table\label{tab1})


// Format Regression Output
//------------------------------------------------------------------------------

import delimited statistics_and_graphs\costs\output_files\m1_partner_EU.csv, clear 

replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"Partner","",1)
replace v1 = subinstr(v1, char(34),"",1)
replace v1 = subinstr(v1, char(34),"",1)

replace v2 = subinstr(v2,"=","",1)
replace v2 = subinstr(v2, char(34),"",1)
replace v2 = subinstr(v2, char(34),"",1)

drop if _n <  4
drop if _n > (_N - 3)
drop v3

destring v1, replace
destring v2, replace

// Merge with Income and save
//------------------------------------------------------------------------------

rename v1 partner
rename v2 m1
gen year = 2004
merge 1:1 partner year using data/gdp_per_capita/gdp_per_capita_defl.dta
keep if _merge == 3
drop _merge
merge 1:1 partner using data/country_codes/ISO3_partner_codes.dta
keep if _merge == 3
drop _merge
gen gdp_log = log(gdp_per_capita)

export delimited statistics_and_graphs\costs\output_files\m1_partner_EU.csv, replace
save statistics_and_graphs\costs\output_files\m1_partner_EU.dta, replace


// Merge cost ad quality data
//------------------------------------------------------------------------------

import delimited statistics_and_graphs\quality\output_files\reg_q_partner.csv, clear 

replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"Partner","",1)
replace v1 = subinstr(v1, char(34),"",1)
replace v1 = subinstr(v1, char(34),"",1)

replace v2 = subinstr(v2,"=","",1)
replace v2 = subinstr(v2, char(34),"",1)
replace v2 = subinstr(v2, char(34),"",1)

drop if _n <  4
drop if _n >  100
drop v3

destring v1, replace
destring v2, replace

rename v1 partner
rename v2 q

merge 1:1 partner using statistics_and_graphs\costs\output_files\m1_partner_EU.dta
keep if _merge == 3
drop _merge
merge 1:1 partner using data\country_codes\ISO3_partner_codes.dta
keep if _merge == 3
drop _merge

export delimited statistics_and_graphs\costs\output_files\m1_q_partner_EU.csv, replace

